package cn.pubinfo.commentapi.controller;

import cn.pubinfo.commentapi.model.Comment;
import cn.pubinfo.commentapi.service.CommentService;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * @author kuancz
 * @date 2018/7/24
 */
@RestController
public class CommentController {
    @Resource
    private CommentService commentService;

    /**
     * getComment
     *
     * @param area     地区
     * @param category 种类，酒店/景点
     * @param id       tourId
     * @param size     每页数量
     * @param page     页码
     * @return commentList
     * @author kun
     * @date 2018-07-26
     */
    @GetMapping("{area}/{category}s/comments/{id:\\d+}")
    public ResponseEntity<List<Comment>> getComment(@PathVariable("area") String area,
                                                    @PathVariable("category") String category,
                                                    @PathVariable("id") Integer id,
                                                    @RequestParam(defaultValue = "20") Integer size,
                                                    @RequestParam(defaultValue = "1") Integer page,
                                                    String start, String end) {
        List<Comment> commentList = commentService.list(area, category, id, page, size, start, end);
        if (commentList == null || commentList.size() == 0) {
            return new ResponseEntity<>(HttpStatus.NOT_FOUND);
        }
        return new ResponseEntity<>(commentList, HttpStatus.OK);
    }

    @GetMapping("{area}/{category}s/comments")
    public ResponseEntity<List<Comment>> listComment(@PathVariable("area") String area,
                                                     @PathVariable("category") String category,
                                                     @RequestParam(defaultValue = "20") Integer size,
                                                     @RequestParam(defaultValue = "1") Integer page,
                                                     String start, String end) {
        List<Comment> commentList = commentService.list(area, category, page, size, start, end);
        if (commentList == null || commentList.size() == 0) {
            return new ResponseEntity<>(HttpStatus.NOT_FOUND);
        }
        return new ResponseEntity<>(commentList, HttpStatus.OK);
    }

    @GetMapping("{area}/{category}s/comments/count")
    public ResponseEntity<Long> count(@PathVariable("area") String area, @PathVariable("category") String category) {
        Long count = commentService.count(area, category);
        return new ResponseEntity<>(count, HttpStatus.OK);
    }

    @GetMapping("{area}/{category}s/comments/count/{id:\\d+}")
    public ResponseEntity<Long> countById(@PathVariable("area") String area, @PathVariable("category") String category, @PathVariable("id") Integer id) {
        Long count = commentService.count(area, category, id);
        return new ResponseEntity<>(count, HttpStatus.OK);
    }


}
